﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbUpgradeOpenGauss.General
{
    /// <summary>
    /// sqlite使用帮助类
    /// </summary>
    public class SqliteHelper
    {
        //private string _connstring = string.Empty;
        ////Data Source=filename;Version=3;Password=myPassword;
        //private SQLiteConnection _conn = null;

        //#region 实例
        ///// <summary>
        ///// 实例
        ///// </summary>
        //private static SqliteHelper _instance;
        ///// <summary>
        ///// 实例
        ///// </summary>
        //public static SqliteHelper Instance
        //{
        //    get
        //    {
        //        if (_instance == null)
        //        {
        //            _instance = new SqliteHelper();
        //        }
        //        return _instance;
        //    }
        //}
        //#endregion

        #region 实例
        /// <summary>
        /// 实例
        /// </summary>
        private static SqliteDal _dbData;
        /// <summary>
        /// 实例
        /// </summary>
        public static SqliteDal DbData
        {
            get
            {
                if (_dbData == null)
                {
                    string path = Path.Combine(CodeHelper.CurrentPath, "dbdata.db");
                    _dbData = new SqliteDal(path);
                }
                return _dbData;
            }
        }
        #endregion

        //private SqliteHelper()
        //{
        //    string path = CodeHelper.CurrentPath;
        //    //string path = CodeHelper.CurrentPath;
        //    _connstring = "Data Source=" + Path.Combine(path, "dbupgrade.db") + ";Version=3;";
        //    _conn = new SQLiteConnection(_connstring);
        //    if (_conn.State == ConnectionState.Closed)
        //    {
        //        _conn.Open();
        //    }
        //}

        //private void OpenConn()
        //{
        //    if (_conn.State == ConnectionState.Closed)
        //    {
        //        _conn.Open();
        //    }
        //}

        //public DataSet ExecDataSet(string sql)
        //{
        //    OpenConn();
        //    SQLiteCommand cmd = _conn.CreateCommand();
        //    cmd.CommandText = sql;
        //    DataSet ds = new DataSet();
        //    SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
        //    da.Fill(ds);
        //    da.Dispose();
        //    cmd.Connection.Close();
        //    cmd.Dispose();
        //    return ds;
        //}

        //public DataTable ExecDataTable(string sql)
        //{
        //    OpenConn();
        //    SQLiteCommand cmd = _conn.CreateCommand();
        //    cmd.CommandText = sql;
        //    DataSet ds = new DataSet();
        //    SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
        //    da.Fill(ds);
        //    da.Dispose();
        //    cmd.Connection.Close();
        //    cmd.Dispose();
        //    DataTable dt = null;
        //    if (ds.Tables.Count > 0)
        //    {
        //        dt = ds.Tables[0];
        //    }
        //    return dt;
        //}

        //public int ExecNoQuery(string sql)
        //{
        //    OpenConn();
        //    SQLiteCommand cmd = _conn.CreateCommand();
        //    cmd.CommandText = sql;
        //    int i = cmd.ExecuteNonQuery();
        //    cmd.Connection.Close();
        //    cmd.Dispose();
        //    return i;
        //}

        //public object ExecScalar(string sql)
        //{
        //    OpenConn();
        //    SQLiteCommand cmd = _conn.CreateCommand();
        //    cmd.CommandText = sql;
        //    object o = cmd.ExecuteScalar();
        //    cmd.Connection.Close();
        //    cmd.Dispose();
        //    return o;
        //}
    }
}
